/*
 * 文 件 名 : SysParameterDAO.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-4-26
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.db;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.fire8.dolphin.model.Answer;
import com.fire8.dolphin.model.Question;
import com.fire8.dolphin.utils.DateUtil;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.StringUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.util.Log;

/**
 * <一句话功能简介><br>
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2012-4-26]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class AnswerDAO
{
    private final String TAG="dolphin";
    private LixgDBHelper dbHelper;
    public AnswerDAO(Context context)
    {
        dbHelper = new LixgDBHelper(context);
    }
    
    public  void add(Answer answer)
    {
        
        if(isExist(answer))return;//如果该回答已存在数据库，则退出
        
        ContentValues values = new ContentValues();
        values.put(Answer.REMOTE_ID, answer.getRemoteId());
        values.put(Answer.Q_ID, answer.getqId());
        values.put(Answer.Q_REMOTE_ID, answer.getqRemoteId());
        values.put(Answer.ANSWER_CONTEXT, answer.getaContext());
        values.put(Answer.UPDATE_DATE, answer.getUpdateDate());
        values.put(Answer.ANSWER_TYPE, answer.getaType());//回答类型，检查提问者是不是自己，如果是，则类型为问题补充0
        values.put(Answer.VOICE, answer.getVoice());//回答内容类型[文字、语音、图片]
        values.put(Answer.USER_ID, answer.getUserId());
        values.put(Answer.USER_NAME, answer.getUserName());
        values.put(Answer.USER_HEAD, answer.getUserHead());
        
        dbHelper.getWritableDatabase().insert(Answer.TABLE_NAME, null, values );
        dbHelper.close();
        
    }
    //检查该记录是否已经存在
    private boolean isExist(Answer answer)
    {
        boolean isExist=false;
        if(StringUtil.isEmpty(answer.getRemoteId()))
        {
            return false;
        }
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Answer.A_ID)//id
        .append(" FROM ")
        .append(Answer.TABLE_NAME)
        .append(" where ").append(Answer.REMOTE_ID).append("=?");
        //.append(" AND ").append(Answer.Q_ID).append("=?");
        args.add(answer.getRemoteId());
        //args.add(answer.getqId());
        LogUtil.d("sql: "+sql.toString());
        //LogUtil.d("参数："+ args.toArray(new String[]{}).toString());
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        if(cursor != null)
        {
            if(cursor.moveToNext())
            {
                isExist=true;
                LogUtil.d("该记录已存在");
                LogUtil.d("aId:"+answer.getRemoteId());
            }
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        return isExist;
    }
    
    public Answer getObject(String id)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Answer.A_ID)//id
        .append(",").append(Answer.REMOTE_ID)//服务器上的ID
        .append(",").append(Answer.Q_ID)//本地问题ID
        .append(",").append(Answer.Q_REMOTE_ID)//远程问题ID
        .append(",").append(Answer.UPDATE_DATE)//回答日期
        .append(",").append(Answer.ANSWER_TYPE)//回答类型[1问题补充，0正常回答]
        .append(",").append(Answer.VOICE)//语音
        .append(",").append(Answer.ANSWER_CONTEXT)//内容
        .append(",").append(Answer.USER_ID)//回答者ID
        .append(",").append(Answer.USER_NAME)//回答者名字
        .append(",").append(Answer.USER_HEAD)
        .append(" FROM ")
        .append(Answer.TABLE_NAME)
        .append(" where 1=1 ");
        if(id != null)
        {
            sql.append(" and ").append(Answer.A_ID).append("=?");
            args.add(id);
        }
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<Answer> list = new ArrayList<Answer>();
        cursor2Object(cursor, list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        if(!list.isEmpty())
        {
            return list.get(0);
        }
        return null;
    }
    
    public List<Answer> findList(Answer answer)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Answer.A_ID)//id
        .append(",").append(Answer.REMOTE_ID)//服务器上的ID
        .append(",").append(Answer.Q_ID)//本地问题ID
        .append(",").append(Answer.Q_REMOTE_ID)//远程问题ID
        .append(",").append(Answer.UPDATE_DATE)//回答日期
        .append(",").append(Answer.ANSWER_TYPE)//回答类型[1问题补充，0正常回答]
        .append(",").append(Answer.VOICE)//语音
        .append(",").append(Answer.ANSWER_CONTEXT)//内容
        .append(",").append(Answer.USER_ID)//回答者ID
        .append(",").append(Answer.USER_NAME)//回答者名字
        .append(",").append(Answer.USER_HEAD)
        .append(" FROM ")
        .append(Answer.TABLE_NAME)
        .append(" where 1=1 ");
        if(answer.getqId() != null)
        {
            sql.append(" and ").append(Answer.Q_ID).append("=?");
            args.add(answer.getqId());
        }
        Log.i(TAG,"查询时 sql: "+sql.toString());
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<Answer> list = new ArrayList<Answer>();
        cursor2Object(cursor, list);
        //dbHelper.cursorToHashMap(cursor, list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        return list;
    }

    /**
     * <一句话功能简介>
     * <功能详细描述>
     * @param cursor
     * @param list [参数说明]
     *
     * @author lixg
     * @see [类、类#方法、类#成员]
     */
    private void cursor2Object(Cursor cursor, List<Answer> list)
    {
        if(cursor != null)
        {
            while(cursor.moveToNext())
            {
                Answer ans = new Answer();
                ans.setaId(cursor.getString(cursor.getColumnIndex(Answer.A_ID)));
                ans.setRemoteId(cursor.getString(cursor.getColumnIndex(Answer.REMOTE_ID)));
                ans.setqId(cursor.getString(cursor.getColumnIndex(Answer.Q_ID)));
                ans.setqRemoteId(cursor.getString(cursor.getColumnIndex(Answer.Q_REMOTE_ID)));
                ans.setUpdateDate(cursor.getString(cursor.getColumnIndex(Answer.UPDATE_DATE)));
                ans.setaType(cursor.getString(cursor.getColumnIndex(Answer.ANSWER_TYPE)));
                ans.setVoice(cursor.getString(cursor.getColumnIndex(Answer.VOICE)));
                ans.setaContext(cursor.getString(cursor.getColumnIndex(Answer.ANSWER_CONTEXT)));
                ans.setUserId(cursor.getString(cursor.getColumnIndex(Answer.USER_ID)));
                ans.setUserName(cursor.getString(cursor.getColumnIndex(Answer.USER_NAME)));
                ans.setUserHead(cursor.getString(cursor.getColumnIndex(Answer.USER_HEAD)));
                list.add(ans);
            }
        }
    }
    
    public void update(Answer answer)
    {
        ContentValues values = new ContentValues();
        values.put(Answer.REMOTE_ID, answer.getRemoteId());
        values.put(Answer.UPDATE_DATE, answer.getUpdateDate());
        values.put(Answer.USER_ID, answer.getUserId());
        String[] args = {String.valueOf(answer.getaId())};
        dbHelper.getWritableDatabase().update(Answer.TABLE_NAME, values,Answer.A_ID+"=?", args);
        dbHelper.close();
    }
    
    public void delete(String id)
    {
        String[] args = {id};
        dbHelper.getWritableDatabase().delete(Answer.TABLE_NAME, Answer.A_ID+"=?", args);
    }
    
}
